loadData <- function(){
	#Load the data 
	trainDataAll <<- read.csv(file="trainFeaturesMerged_number_300.csv",sep=",", head = FALSE)
	testDataAll <<-   read.csv(file = "testFeaturesMerged_number_300.csv",sep=",", head = FALSE)
}
featureSelection <- function(){
	# Features Selection 
	library(FSelector)
	library(mlbench)
	#weights <- random.forest.importance(V648~., data=trainDataAll)
	weights <- information.gain(V305~., data=trainDataAll)
	# Use only 70 features
	subSet <- cutoff.k(weights,70)
	testData <<- subset(testDataAll, select = subSet)
	subSet <- c("V305",subSet)
	trainData <<- subset(trainDataAll,select =subSet)	
}
trainModel <- function(){
	# Train the model 
	x <- subset(trainData, select = -V305)
	y <- trainData$V305
	library('e1071')
	model <<- svm(x, y, type = "C", cost = 32, gamma = 2^-9, weight)	
	#model <<- svm(x, y, type = "C", cross = 5, cost = 32, gamma = 2^-9)	
	#print (summary(model))
}
printResult <- function(){
	# Evaluate the result 
	pred <- predict(model,testData)
	result <- {}
	#print (pred)
	for (i in 1:length(pred)){
		if (pred[i] == 1) result[i] <- "population"
		if (pred[i] == 2) result[i] <- "intervention"
		if (pred[i] == 3) result[i] <- "background"
		if (pred[i] == 4) result[i] <- "outcome"
		if (pred[i] == 5) result[i] <- "study design"
		if (pred[i] == 6) result[i] <- "other"
	}
	#print (result)
	######### OK now load the file #####################
	testFile <- read.csv(file="test.csv", sep=",", head = TRUE)
	count <- 1 
	Prediction <- {}
	for (label in testFile[,1]){
		count <- count + 1 
		index <- (count -2) / 6
		rightLb <- result[index +1]
		if (rightLb == label){
			Prediction <- c(Prediction,"Yes")
		}
		else {
			Prediction <- c(Prediction,"No")
		}

	}
	############## Now merge with original test file #########
	outputFile <<- cbind(testFile,Prediction)
	#write.table(outputFile,file="output_SVM_featureSelection.csv", sep=",",row.names=FALSE)
	write.csv(outputFile,file="output_SVM_featureSelection.csv",row.names=FALSE)
}

loadData()
print (" Done Load data ") 
featureSelection()
print (" Done feature selection")
trainModel()
print (" Done model training")
printResult()

# Load the test file 


# tune it 
#obj <- tune (svm, V314 ~., data = trainDataAll, range=list(cost = c(10^(-5:5))))

